import 'package:pax_info_app/utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'dart:core';

class Resp<T> {
  String? message;
  T? data;
  int? code;

  bool valid() {
    return code == 200;
  }

  void showErr()
  {
    if(valid()) return;
    // if (code == 401) {
    //   return;
    // }
    String msg = message??"未知错误";
    CommonUtils.toast(msg);
  }

  Resp({this.message,this.data, this.code});

  Resp.fromJson(Map<String, dynamic> json) {
    print('=== json: $json');
    this.code = json['Code'].runtimeType == int ? json['Code'] : int.parse(json['Code']);
    this.message = json['Message'];
    this.data = this.valid() ? json['Data'] : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['message'] = this.message;
    data['data'] = this.data;
    return data;
  }

}


class OpRes extends Resp<dynamic> {

  OpRes.fromJson(Map<String, dynamic> json) {
    this.code = json['code'].runtimeType == int ? json['code'] : int.parse(json['code']);
    this.message = json['message'];
    this.data = this.valid() ? json['data'] : null;
  }
}

class RespLogin<T> {
  bool? status;
  int? code;
  String? message;
  dynamic? data;

  // 请求是否成功
  bool valid() {
    return code == 0 && status == true;
  }

  void showErr() {
    if(valid()) return;
    String msg = message??"未知错误";
    CommonUtils.toast(msg);
  }

  RespLogin({this.message, this.data, this.code, this.status,});

  RespLogin.fromJson(Map<String, dynamic> json) {
    print('json: $json');
    this.status = json['Status']??false;
    this.code = json['Code'].runtimeType == int ? json['Code'] : int.parse(json['Code']);
    this.message = json['Message'] != null ? json['Message'] : '';
    this.data = this.valid() ? json['Data'] : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['status'] = this.status;
    data['code'] = this.code;
    data['message'] = this.message;
    data['data'] = this.data;
    return data;
  }

}